package ast;

import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Node;
import com.github.javaparser.printer.YamlPrinter;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.Reader;

public class Main {
    public static void main(String[] args) throws FileNotFoundException {

        // 解析字符串
//        CompilationUnit cu = StaticJavaParser.parse("class X { int x; }");
//        System.out.println(cu);
//
//        YamlPrinter printer = new YamlPrinter(true);
//        System.out.println(printer.output(cu));

        // 读取文件并解析
//        Reader reader = new FileReader("C:\\Users\\jinbin\\Code\\exp\\src\\main\\java\\jgit\\Main.java");
//        CompilationUnit cu2 = StaticJavaParser.parse(reader);
//        System.out.println(cu2);
//
//        YamlPrinter printer = new YamlPrinter(true);
//        System.out.println(printer.output(cu2));

        // The base: iterators
        // Breadth-first traversal will visit the start node,
        // then all its direct children,
        // then these children’s direct children, and so on. It’s honestly not very useful.
        Reader reader1 = new FileReader("C:\\Users\\jinbin\\Code\\exp\\src\\main\\java\\ast\\sample.java");
        CompilationUnit cu3 = StaticJavaParser.parse(reader1);

        // 以
//        Node.BreadthFirstIterator iterator = new Node.BreadthFirstIterator(cu3);
//        while (iterator.hasNext()) {
//            System.out.println("* " + iterator.next());
//        }

        // 以Yaml的方式输出
        YamlPrinter printer = new YamlPrinter(true);
        System.out.println(printer.output(cu3));
    }
}
